package com.yyn.Learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.Learning.client.VodClient;
import com.yyn.Learning.entity.EduChapter;
import com.yyn.Learning.entity.EduVideo;
import com.yyn.Learning.mapper.EduVideoMapper;
import com.yyn.Learning.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.R;
import com.yyn.servicebase.exceptionhandler.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2022-11-22
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    //    注入Vod(接口暴露)
    @Autowired
    private VodClient vodClient;

    @Override
    public void saveVideo(EduVideo eduVideo) {
        this.save(eduVideo);
    }

    @Override
    public void removeVideo(String id) {
        this.removeById(id);
    }

    @Override
    public void updateVideo(EduVideo eduVideo) {
        this.updateById(eduVideo);
    }

    @Override
    public void getAllVideo() {
        this.list();
    }

    @Override
    public EduVideo getOneVideo(String id) {
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getId, id);
        return this.getOne(eduVideoLambdaQueryWrapper);

    }

    //    通过课程id删视频  一个课程里有多个小节 即多个视频
    @Override
    public void removeByCourseId(String courseId) {
        LambdaQueryWrapper<EduVideo> eduVideoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        eduVideoLambdaQueryWrapper.eq(EduVideo::getCourseId, courseId);//找小节
        eduVideoLambdaQueryWrapper.select(EduVideo::getId);//只显示小节iD
        List<EduVideo> list = this.list(eduVideoLambdaQueryWrapper);
        if(list.size()==0){
//           无小节 不用删了
            return;
        }
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            strings.add(list.get(i).getId());//得到所有小节id
        }
            //        把这个id列表换为字符串 ，分隔
            String videoIds = StringUtils.join(strings.toArray(), ",");
            this.removeTwo(videoIds);//字符串方式把所有id传过去



    }

    //删小节删视频 传过来的肯定是小节id
    @Override
    public void removeTwo(String id) {
        if (id.indexOf(",") != -1) { //id里存在，
            String[] ids = id.split(",");
            for (int i = 0; i < ids.length; i++) {//循环把视频全部删掉
//                1、通过小节id找到 视频id
                EduVideo oneVideo = this.getOneVideo(ids[i]);
                String videoSourceId = oneVideo.getVideoSourceId();
                if (StringUtils.isNoneEmpty(videoSourceId)) {
//            删视频
                    R r = vodClient.removeVideo(videoSourceId);
                    if(r.getCode()==2001){
                        throw new CustomException(20001,"删除视频失败，熔断机制..");
                    }
                }
                //        删小节
                this.removeById(ids[i]);
            }

        } else {
//        这里是单删
//                1、通过小节id找到 视频id
            EduVideo oneVideo = this.getOneVideo(id);
            String videoSourceId = oneVideo.getVideoSourceId();
            if (StringUtils.isNoneEmpty(videoSourceId)) {
//            删视频
                R r = vodClient.removeVideo(videoSourceId);
                if(r.getCode()==2001){
                    throw new CustomException(20001,"删除视频失败，熔断机制..");
                }
            }
//        删小节
            this.removeById(id);
        }
    }
}
